{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 10\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically._**\n",
    "\n",
    "> **_Suppose the following input is supplied to the program:_**\n",
    "\n",
    "hello world and practice makes perfect and hello world again\n",
    "\n",
    "> **_Then, the output should be:_**\n",
    "\n",
    "again and hello makes perfect practice world\n",
    "\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input.We use set container to remove duplicated data automatically and then use sorted() to sort the data._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = input().split()\n",
    "\n",
    "for i in word:\n",
    "    if (\n",
    "        word.count(i) > 1\n",
    "    ):  # count function returns total repeatation of an element that is send as argument\n",
    "        word.remove(i)  # removes exactly one element per call\n",
    "\n",
    "word.sort()\n",
    "print(\" \".join(word))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = input().split()\n",
    "[\n",
    "    word.remove(i) for i in word if word.count(i) > 1\n",
    "]  # removal operation with comprehension method\n",
    "word.sort()\n",
    "print(\" \".join(word))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = sorted(\n",
    "    list(set(input().split()))\n",
    ")  #  input string splits -> converting into set() to store unique\n",
    "#  element -> converting into list to be able to apply sort\n",
    "print(\" \".join(word))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 11\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence._**\n",
    "\n",
    "> **_Example:_**\n",
    "\n",
    "0100,0011,1010,1001\n",
    "\n",
    "> **_Then the output should be:_**\n",
    "\n",
    "1010\n",
    "\n",
    "> **_Notes: Assume the data is input by console._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check(x):  # converts binary to integer & returns zero if divisible by 5\n",
    "    total, pw = 0, 1\n",
    "    reversed(x)\n",
    "\n",
    "    for i in x:\n",
    "        total += pw * (ord(i) - 48)  # ord() function returns ASCII value\n",
    "        pw *= 2\n",
    "    return total % 5\n",
    "\n",
    "\n",
    "data = input().split(\",\")  # inputs taken here and splited in ',' position\n",
    "lst = []\n",
    "\n",
    "for i in data:\n",
    "    if check(i) == 0:  # if zero found it means divisible by zero and added to the list\n",
    "        lst.append(i)\n",
    "\n",
    "print(\",\".join(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check(x):  # check function returns true if divisible by 5\n",
    "    return int(x, 2) % 5 == 0  # int(x,b) takes x as string and b as base from which\n",
    "    # it will be converted to decimal\n",
    "\n",
    "\n",
    "data = input().split(\",\")\n",
    "\n",
    "data = list(\n",
    "    filter(check, data)\n",
    ")  # in filter(func,object) function, elements are picked from 'data' if found True by 'check' function\n",
    "print(\",\".join(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = input().split(\",\")\n",
    "data = list(\n",
    "    filter(lambda i: int(i, 2) % 5 == 0, data)\n",
    ")  # lambda is an operator that helps to write function of one line\n",
    "print(\",\".join(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 12\n",
    "\n",
    "### **Question:**\n",
    "\n",
    "> **_Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.The numbers obtained should be printed in a comma-separated sequence on a single line._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = []\n",
    "\n",
    "for i in range(1000, 3001):\n",
    "    flag = 1\n",
    "    for j in str(i):  # every integer number i is converted into string\n",
    "        if ord(j) % 2 != 0:  # ord returns ASCII value and j is every digit of i\n",
    "            flag = 0  # flag becomes zero if any odd digit found\n",
    "    if flag == 1:\n",
    "        lst.append(str(i))  # i is stored in list as string\n",
    "\n",
    "print(\",\".join(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check(element):\n",
    "    return all(\n",
    "        ord(i) % 2 == 0 for i in element\n",
    "    )  # all returns True if all digits i is even in element\n",
    "\n",
    "\n",
    "lst = [\n",
    "    str(i) for i in range(1000, 3001)\n",
    "]  # creates list of all given numbers with string data type\n",
    "lst = list(\n",
    "    filter(check, lst)\n",
    ")  # filter removes element from list if check condition fails\n",
    "print(\",\".join(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [str(i) for i in range(1000, 3001)]\n",
    "lst = list(\n",
    "    filter(lambda i: all(ord(j) % 2 == 0 for j in i), lst)\n",
    ")  # using lambda to define function inside filter function\n",
    "print(\",\".join(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 13\n",
    "\n",
    "### **Question:**\n",
    "\n",
    "> **_Write a program that accepts a sentence and calculate the number of letters and digits._**\n",
    "\n",
    "> **_Suppose the following input is supplied to the program:_**\n",
    "\n",
    "hello world! 123\n",
    "\n",
    "> **_Then, the output should be:_**\n",
    "\n",
    "LETTERS 10\n",
    "\n",
    "DIGITS 3\n",
    "\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = input()\n",
    "letter, digit = 0, 0\n",
    "\n",
    "for i in word:\n",
    "    if (\"a\" <= i and i <= \"z\") or (\"A\" <= i and i <= \"Z\"):\n",
    "        letter += 1\n",
    "    if \"0\" <= i and i <= \"9\":\n",
    "        digit += 1\n",
    "\n",
    "print(\"LETTERS {0}\\nDIGITS {1}\".format(letter, digit))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = input()\n",
    "letter, digit = 0, 0\n",
    "\n",
    "for i in word:\n",
    "    if i.isalpha():  # returns True if alphabet\n",
    "        letter += 1\n",
    "    elif i.isnumeric():  # returns True if numeric\n",
    "        digit += 1\n",
    "print(\n",
    "    f\"LETTERS {letter}\\n{digits}\"\n",
    ")  # two different types of formating method is shown in both solution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "**_All the above problems are mostly string related problems. Major parts of the solution includes string releted functions and comprehension method to write down the code in more shorter form._**"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
